package com.strobel.decompiler.languages.java.ast;

import java.util.HashMap;
import java.util.Map;

// use for android resource id
public class ResourceId {
    private static Map<Integer, String> ids = new HashMap<Integer, String>();
    private static boolean inRClass = false;
    
    public static void startRClass() {
        inRClass = true;
    }
    
    public static void endRClass() {
        inRClass = false;
    }
    
    public static boolean isInRClass() {
        return inRClass;
    }
    
    public static void addId(String name, Object value) {
        if(!inRClass)
            return;
        if(!(value instanceof Integer))
            return;
        Integer v = (Integer)value;
        if(v < 0x7f000000)
            return;
        if(ids.get(v) != null)
            return;

        int idx = name.indexOf("/R$");
        if(idx < 0)
            idx = name.indexOf("/R.");
        if(idx < 0)
            return;
        String varFullName = name.substring(idx+1).replace('$', '.').replace('/', '.');
        System.err.println("resource id: " + varFullName + " = " + v);
        ids.put(v, varFullName);
    }
    
    public static String getResName(Object value) {
        if(!(value instanceof Integer)) {
            try {
                value = Integer.parseInt((String)value);
            } catch (Exception e) {
                return null;
            }
        }
        return ids.get(value);
    }
}
